var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
    type:'juli',
		longitude:'',
		latitude:'',
		datalist: [],
		pagenum: 1,
		keyword:'',
		cid:0,
	},
	_onLoad:function(){
		var that = this
		if(that.options && that.options.cid){
			that.setData({cid:that.options.cid});
		}
    $.get('business/blist', {  }, function (res) {
        that.setData(res);
    });
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
		wx.getLocation({
			type: 'gcj02',
			success:function(res) {
				var latitude = res.latitude
				var longitude = res.longitude
				that.setData({longitude:longitude,latitude:latitude})
				that.getDataList();
			},
			fail:function(){
				that.getDataList();
			}
		})
	},
	getDataList: function () {
		var that = this
		var pagenum = that.data.pagenum
		var latitude = that.data.latitude
		var longitude = that.data.longitude
		var keyword = that.data.keyword
		console.log(keyword)
		that.setData({loaddingmore:true})
    $.post('business/blist', { pagenum: pagenum, cid: that.data.cid, field:that.data.type,order:'desc',longitude:longitude,latitude:latitude,keyword:keyword}, function (res) {
			that.setData({loaddingmore:false})
			var data = res.data
			if (pagenum == 1) {
				that.setData({ datalist: data });
				if (data.length == 0) {
					$.showEmpty('暂无相关数据！');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
			that.setData({
				sclist: data
			});
		},false);
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
	},
  changetab:function(e){
    var that = this;
    var cid = e.currentTarget.dataset.cid
    that.setData({ cid: cid })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
    //this.onLoad()
		that.getDataList();
  },
	search:function(e){
    var that = this;
		var keyword = e.detail.value;
    that.setData({ keyword: keyword })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
		that.getDataList();

	},
  sortClick:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type
    that.setData({ type: type })
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
    that.getDataList();
  },
  filterClick:function(e){
    var that = this;
    var types = e.currentTarget.dataset.types
    that.setData({ types: types })

  }
}))